/*
 * xsimplepager
 *
 * Copyright (C) 2016 Alexander Andrejevic <theflash AT sdf DOT lonestar DOT org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>

#define WNCK_I_KNOW_THIS_IS_UNSTABLE
#include <libwnck/libwnck.h>

static gint x = 0;
static gint y = 0;
static gint width = 100;
static gint height = 24;
static gint rows = 1;
static gboolean vertical = FALSE;
static gdouble red = 0.2;
static gdouble green = 0.2;
static gdouble blue = 0.2;
static gdouble alpha = 0.8;

static const GOptionEntry options[] = {
    { "x", 'x', G_OPTION_FLAG_NONE, G_OPTION_ARG_INT, &x, "The X coordinate of the pager, in pixels.", NULL },
    { "y", 'y', G_OPTION_FLAG_NONE, G_OPTION_ARG_INT, &y, "The Y coordinate of the pager, in pixels.", NULL },
    { "width", 'w', G_OPTION_FLAG_NONE, G_OPTION_ARG_INT, &width, "The width of the pager, in pixels.", NULL },
    { "height", 'h', G_OPTION_FLAG_NONE, G_OPTION_ARG_INT, &height, "The height of the pager, in pixels.", NULL },
    {
        "rows", 'r', G_OPTION_FLAG_NONE, G_OPTION_ARG_INT, &rows,
        "The number of rows in which workspace thumbnails should be displayed.", NULL
    },
    {
        "horizontal", '\0', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &vertical,
        "Make the pager horizontal (the default).", NULL
    },
    { "vertical", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_NONE, &vertical, "Make the pager vertical.", NULL },
    {
        "red", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_DOUBLE, &red,
        "The red component of the background color, between 0 and 1 (default: 0.2).", NULL
    },
    {
        "green", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_DOUBLE, &green,
        "The green component of the background color, between 0 and 1 (default: 0.2).", NULL
    },
    {
        "blue", 'v', G_OPTION_FLAG_NONE, G_OPTION_ARG_DOUBLE, &blue,
        "The blue component of the background color, between 0 and 1 (default: 0.2).", NULL
    },
    {
        "alpha", 'a', G_OPTION_FLAG_NONE, G_OPTION_ARG_DOUBLE, &alpha,
        "The alpha component of the background color, between 0 and 1 (default: 0.8).", NULL
    },
    { NULL, '\0', 0, 0, NULL, NULL, NULL }
};

static gboolean on_pager_expose(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
    cairo_t *cr = gdk_cairo_create(widget->window);

    cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
    cairo_set_source_rgba(cr, red, green, blue, alpha);
    cairo_paint(cr);

    cairo_destroy(cr);
    return FALSE;
}

int main(int argc, char *argv[])
{
    gtk_init(&argc, &argv);

    GOptionContext *context = g_option_context_new(" - Display a pager on the screen.");
    g_option_context_add_main_entries(context, options, NULL);

    GError *error = NULL;
    if (!g_option_context_parse(context, &argc, &argv, &error))
    {
        fprintf(stderr, "Error: %s\n", error->message);
        g_error_free(error);
        g_option_context_free(context);
        return 2;
    }

    g_option_context_free(context);

    GtkWindow *window = GTK_WINDOW(gtk_window_new(GTK_WINDOW_POPUP));
    gtk_window_stick(window);
    gtk_window_move(window, x, y);

    GdkScreen *screen = gtk_widget_get_screen(GTK_WIDGET(window));
    GdkColormap *rgba = gdk_screen_get_rgba_colormap(screen);
    gtk_widget_set_colormap(GTK_WIDGET(window), rgba);

    WnckPager *pager = WNCK_PAGER(wnck_pager_new(wnck_screen_get_default()));
    gtk_widget_set_size_request(GTK_WIDGET(pager), width, height);
    wnck_pager_set_orientation(pager,  vertical ? GTK_ORIENTATION_VERTICAL : GTK_ORIENTATION_HORIZONTAL);
    wnck_pager_set_n_rows(pager, rows);
    wnck_pager_set_display_mode(pager, WNCK_PAGER_DISPLAY_CONTENT);
    g_signal_connect(G_OBJECT(pager), "expose-event", G_CALLBACK(on_pager_expose), NULL);
    gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(pager));

    gtk_widget_show_all(GTK_WIDGET(window));
    gtk_main();

    return 0;
}
